package com.bridgeintelligent.tag.webserver.dim.mgmt.service.es;

import com.bridgeintelligent.tag.mapper.EsTimeDataMapper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.EsTimeData;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.EsTimeModel;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description
 * @author: grp 2021-01-13 15:09
 **/
@AllArgsConstructor
@Service
public class EsInfoServiceImpl implements IEsInfoService{

    private EsTimeDataMapper esTimeDataMapper;

    @Override
    public String dataTime() {
        EsTimeData realTime = esTimeDataMapper.findRealTime();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDate = LocalDate.parse(realTime.getTime(),dateTimeFormatter);
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return localDate.format(dateTimeFormatter2)+" 23:59:59";
    }

    @Override
    public String indexDataTime(String indexId) {
        EsTimeData realTime = esTimeDataMapper.findRealTimeByIndexId(indexId);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDate = LocalDate.parse(realTime.getTime(),dateTimeFormatter);
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return localDate.format(dateTimeFormatter2)+" 23:59:59";
    }

    @Override
    public Map<String, String> symbolRelation(String indexId) {
        List<EsTimeModel> timeList = esTimeDataMapper.indexAllTime(indexId);
        Map<String,String> map = timeList.stream().collect(Collectors.toMap(EsTimeModel::getSymbol,EsTimeModel::getEsSymbol));
        return map;
    }

}
